package com.leetcode.LC;

import java.util.HashMap;
import java.util.Map;

public class LC219 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {

        Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (hashMap.containsKey(nums[i])){
                if(i-hashMap.get(nums[i])<=k){
                    return true;
                }else {
                    hashMap.put(nums[i], i);
                }
            }else {
                hashMap.put(nums[i], i);
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(new LC219().containsNearbyDuplicate(new int[]{1,0,2,1,5,1}, 2));
    }
}
